package com.agnes.network.util;

import android.text.TextUtils;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 * desc:
 * creator: yubaolin
 * date:    2016/9/9/0009
 */
public class JsonUtils {
    private static final String TAG = "JsonUtils==>>";

    public static boolean isJOFormat(String jsonStr) {
        return !TextUtils.isEmpty(jsonStr) && jsonStr.trim().startsWith("{") && jsonStr.trim()
                                                                                       .endsWith("}");
    }

    public static boolean isJAFormat(String jsonStr) {
        return !TextUtils.isEmpty(jsonStr) && jsonStr.trim().startsWith("[") && jsonStr.trim()
                                                                                       .endsWith("]");
    }

    public static JSONObject parseJO(String jsonStr) {
        try {
            if (isJOFormat(jsonStr)) return new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static JSONArray parseJA(String jsonStr) {
        try {
            if (isJAFormat(jsonStr)) return new JSONArray(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Gson 解析字符串为实体类
     */
    public static <T> T toBean(String str, Class<T> cla) {
        if (isJOFormat(str)) {
            return new Gson().fromJson(str, cla);
        }

        return null;
    }

    /**
     * Gson 解析字符串为实体类
     */
    public static <T> T toBean(JSONObject jsonObject, Class<T> cla) {
        if (jsonObject == null) return null;
        return toBean(jsonObject.toString(), cla);
    }

    public static <T> String toJson(T t) {
        if (t != null) {
            return new Gson().toJson(t);
        }
        return "{}";
    }

    /**
     * 只能返回参数是String的List
     *
     * @param jsonArray
     * @return
     */
    public static List<String> parseJsonArrayToList(JSONArray jsonArray) {
        List<String> list = new ArrayList<>();
        if (jsonArray != null && jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                list.add(jsonArray.optString(i));
            }
        }
        return list;
    }

    public static Map<String, String> toMap(JSONObject jsonObject) {
        Map<String, String> map = new HashMap<>();
        if (jsonObject != null) {
            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = jsonObject.optString(key);
                map.put(key, value);
            }
        }

        return map;
    }

    public static JSONObject map2Json(Map<String, String> jsonMap) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        if (jsonMap != null && !jsonMap.isEmpty()) {
            for (Map.Entry<String, String> entry : jsonMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                builder.append(key).append(":").append(value).append(",");
            }
            builder.deleteCharAt(builder.length()-1);
        }
        builder.append("}");
        return JsonUtils.parseJO(builder.toString());
    }
}
